package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import core.constants.Constants;
import views.util.GameColors;

public class SchiacciaAranciaView extends GameView implements ActionListener {										
	private Timer cronometro = new Timer(1000, this);
	private JButton[][] coverCells;
	private JButton[][] bagCells;
	private int dimX, dimY;
	private int timeMax = Constants.getGameTime();

	private JPanel main;
	private JPanel coverPanel;
	private JPanel bagPanel;

	private int gameCount = 1;
	private int scalingFactorX;
	private int scalingFactorY;
	private GridBagConstraints gbc = new GridBagConstraints();
	private GridBagConstraints gbcBottom = new GridBagConstraints();
	private ActionListener currentActionListener;

	public SchiacciaAranciaView(int dimX, int dimY) {
		super(GameColors.AZZURRO, GameColors.AZZURRO_CHIARO, "1:00",
				"PUNTEGGIO: 0", "VALIGIA 1");
		this.dimX = dimX;
		this.dimY = dimY;
		coverCells = new JButton[dimY][dimX];
		bagCells = new JButton[dimY][dimX];
		main = new MainPanel();
		gbc.insets = new Insets(30, 0, 0, 0);
		main.setLayout(new GridBagLayout());
		main.setBackground(GameColors.AZZURRO);
		add(main, BorderLayout.CENTER);
		coverPanel = buildCoverPanel();
		bagPanel = buildBagPanel();

		if (dimX == dimY) {
			scalingFactorX = 60;
			scalingFactorY = 60;
		}
		if (dimX < dimY) {
			scalingFactorX = 80;
			scalingFactorY = 40;
		}

		Dimension dim = new Dimension(dimX * scalingFactorX, dimY
				* scalingFactorY);
		coverPanel.setPreferredSize(dim);
		bagPanel.setPreferredSize(dim);
		gbc.gridx = 0;
		gbc.gridy = 0;
		main.add(coverPanel, gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		main.add(bagPanel, gbc);
		cronometro.start();
	}

	private class MainPanel extends JPanel {

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.fillRect((getWidth() / 2 - 5) + 29, getHeight() / 2 - 10, 18, 40);
			g.fillRect((getWidth() / 2 - 5) - 35, getHeight() / 2 - 10, 18, 40);
		}
	}

	private JPanel buildCoverPanel() {
		JPanel cover = new JPanel();
		cover.setLayout(new GridLayout(dimY, dimX, 1, 1));
		cover.setBackground(GameColors.AZZURRO);
		for (int i = 0; i < dimY; i++) {
			for (int j = 0; j < dimX; j++) {
				coverCells[i][j] = new JButton();
				coverCells[i][j].setName(i + "-" + j);
				coverCells[i][j].setBackground(Color.white);
				cover.add(coverCells[i][j]);

			}
		}
		return cover;
	}

	private JPanel buildBagPanel() {
		JPanel bag = new JPanel();
		bag.setLayout(new GridLayout(dimY, dimX, 1, 1));
		bag.setBackground(GameColors.AZZURRO);
		for (int i = 0; i < dimY; i++) {
			for (int j = 0; j < dimX; j++) {
				bagCells[i][j] = new JButton();
				bagCells[i][j].setName(i + "-" + j);
				bagCells[i][j].setBackground(Color.white);
				bag.add(bagCells[i][j]);
			}
		}
		return bag;
	}

	public JButton[][] getCoverCells() {
		return coverCells;
	}

	public JButton[][] getBagCells() {
		return bagCells;
	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
		for (int i = 0; i < dimY; i++) {
			for (int j = 0; j < dimX; j++) {
				coverCells[i][j].addActionListener(al);
			}
		}

	}

	public void removeActionListenerToGame(ActionListener al) {
		for (int i = 0; i < dimY; i++) {
			for (int j = 0; j < dimX; j++) {
				coverCells[i][j].removeActionListener(al);
			}
		}
	}

	public void refresh(int dimX, int dimY) {
		this.dimX = dimX;
		this.dimY = dimY;
		coverCells = new JButton[dimY][dimX];
		bagCells = new JButton[dimY][dimX];
		gbc.insets = new Insets(30, 0, 0, 0);
		main.remove(coverPanel);
		main.remove(bagPanel);
		coverPanel = buildCoverPanel();
		bagPanel = buildBagPanel();

		if (dimX == dimY) {
			scalingFactorX = 60;
			scalingFactorY = 60;
		}
		if (dimX < dimY) {
			scalingFactorX = 80;
			scalingFactorY = 40;
		}
		Dimension dim = new Dimension(dimX * scalingFactorX, dimY
				* scalingFactorY);
		coverPanel.setPreferredSize(dim);
		bagPanel.setPreferredSize(dim);
		gbc.gridx = 0;
		gbc.gridy = 0;
		main.add(coverPanel, gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		main.add(bagPanel, gbc);
		main.repaint();
	}

	public void setScore(int x) {
		score.setText("PUNTEGGIO: " + x);

	}

	public void setBagCount(int x) {
		other.setText("VALIGIA: " + x);
		gameCount = x;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		timeMax--;
		gameClock.setText("0:" + timeMax);
		if (timeMax == 0) {
			removeActionListenerToGame(currentActionListener); // per evitare bug nell'ultimo aggiornamento della view
			main.setCursor(Cursor.getDefaultCursor());
			cronometro.stop();
			gameEnded();
		}

	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		gameClock.setVisible(false);
		main.remove(coverPanel);
		main.remove(bagPanel);
		JLabel end = newLabel("Partita Conclusa!", GameColors.AZZURRO_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
		updateUI();
	}

	public void addMouseListenerToGame(MouseListener ml) {
		main.addMouseListener(ml);

	}

}
